﻿using CBS.APP;
using CBS.Desktop.FormPage;
using CBS.Model.ManageModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CBS.Desktop.ViewPartial
{
    /// <summary>
    /// SemesterList.xaml 的交互逻辑
    /// </summary>
    public partial class SemesterList : UserControl
    {
        public Pagination pager = new() { rows = 10, page = 1, sidx = nameof(SemesterViewModel.SemesterId), sord = "desc" };

        public SemesterList()
        {
            InitializeComponent();

            FlushGrid();
            
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // 调用处理方法
                pager.page = 1;
                pg_DataList.PageIndex = 1;
                FlushGrid();
                e.Handled = true; // 如果需要，标记事件已处理
            }
        }
        private async void FlushGrid()
        {
            try
            {
                btn_Search.IsEnabled = false;
                btn_Add.IsEnabled = false;

                string key = tb_SearchKey.Text.Trim();
                var semesters = await Task.Run(() => SemesterApp.GetList(pager, key));

                this.Dispatcher.Invoke(() =>
                {
                    pg_DataList.MaxPageCount = pager.total;
                    dgr_DataList.ItemsSource = semesters;

                    btn_Search.IsEnabled = true;
                    btn_Add.IsEnabled = true;
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
                btn_Search.IsEnabled = true;
                btn_Add.IsEnabled = true;
            }

        }

        private void btn_Search_Click(object sender, RoutedEventArgs e)
        {
            pager.page = 1;
            pg_DataList.PageIndex = 1;
            FlushGrid();
        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            AddSemester addSemester = new()
            {
                Owner = Window.GetWindow(this)
            };
            var dialogResult = addSemester.ShowDialog(); 
            if (dialogResult.HasValue && dialogResult.Value)
            {
                pager.page = 1;
                pg_DataList.PageIndex = 1;
                FlushGrid();
            }
        }

        private void btn_Delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string key = tb_SearchKey.Text.Trim();
                if (SemesterApp.BatchDeleteSemester(key, out string msg))
                {
                    pager.page = 1;
                    pg_DataList.PageIndex = 1;
                    FlushGrid();
                }
                else
                {
                    throw new Exception(msg);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void pg_DataList_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
        {
            pager.page = e.Info;
            string key = tb_SearchKey.Text.Trim();
            FlushGrid();
        }

        private void dg_Delete(object sender, RoutedEventArgs e)
        {
            try
            {
                if (sender is Button button && button.DataContext is SemesterViewModel data)
                {
                    MessageBoxResult result = MessageBox.Show($"是否确定删除?", "警告", MessageBoxButton.YesNo);
                    if (result == MessageBoxResult.Yes)
                    {
                        if (SemesterApp.DeleteSemester(data.SemesterId, out string msg))
                        {
                            FlushGrid();
                        }
                        else 
                        {
                            throw new Exception(msg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void dg_Edit(object sender, RoutedEventArgs e)
        {
            try
            {
                if (sender is Button button && button.DataContext is SemesterViewModel data)
                {
                    AddSemester addSemester = new(data.SemesterId)
                    {
                        Owner = Window.GetWindow(this)
                    };
                    var dialogResult = addSemester.ShowDialog();
                    if (dialogResult.HasValue && dialogResult.Value)
                    {
                        FlushGrid();
                    }
                
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }


    }
}
